<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
// use app\common\jssdk\Jssdk;
class Index extends Controller{

    public function index() {
        $this->redirect('admin/activity/index');
    }

	public function initApi($token = '')
	{	
		$request = request();
		$db = db();
		$token = $request->post('token');
		if (!isset($token)) {
			$res['code'] = 0;
			$res['error'] = '参数错误';
		}else{
			$result = $db->name('wechat_user')->field("id,openid,token,score")->where("token=".$token)->find();
			if ($result) {
				$res['code'] = 1;
				$res['msg'] = 'success'; 
				$res['success'] = true; 
				$res['result'] = $result;
				//继续查询set表中的设置
				$sresult = $db->name('set')->field('id,bg_img')->find();
				$res['set_img'] = $sresult['bg_img']; 
			}
		}
		return json($res);
	}

	// 闯关模式api
	public function breakApi($id = 2){
		$db = db();
		$mcount = $db->name('cont')->field("COUNT(id) ids")->find();
		$mcount = $mcount['ids'];
		//  基本的写好，可以以后优化
		if ($id < 0 || $id > $mcount) {
			$res['code'] = 0;
			$res['msg'] = '访问出错';
			return json($res);
		}
		//访问数据库
		$result = $db->name('cont')->alias('c')->join('word w','c.id=w.mid')->field('c.id,c.answer,c.music,c.music2,c.spend,w.text_word')->where('c.id='.$id)->select();
		if ($result) {
			$res['code'] = 1;
			$res['msg'] = 'success'; 
			$res['success'] = true; 
			$res['result'] = $result;
		}
		//$str = '白,玫,瑰,浮,肯,对,说,舞,这,杰,猜,管,是,呀,这,进,放,车,夸,而,速'; 格式
		// 
		$str_text = explode(',', $res['result'][0]['text_word']);
		for ($i=0; $i < count($str_text); $i++) { 
			$datas[] = $str_text[$i];
		}
		$res['result'][0]['confound'] = $datas;
		return json($res);
	}
	// 排行api
	public function rankApi(){
		$db = db();
		$result = $db->name('rank')->field("pai,user_id,user_name,user_img,victory,fail")->order('victory desc')->select();
		if (!empty($result)) {
			$res['code'] = 1;
			$res['msg'] = 'success';
			$res['success'] = true; 
			$res['result'] = $result;
		}else{
			$res['code'] = 0;
			$res['msg'] = '访问出错';
		}
		return json($res);
	}
	// 小助手api
	public function aideApi(){
		$db = db();
		$result = $db->name('aide')->field("id,wen,da")->select();
		if (!empty($result)) {
			$res['code'] = 1;
			$res['msg'] = 'success';
			$res['success'] = true; 
			$res['result'] = $result;
		}else{
			$res['code'] = 0;
			$res['msg'] = '访问出错';
		}
		return json($res);
	}
	// 更多游戏api
	public function moregameApi(){
		$db = db();
		$result = $db->name('moregame')->field("id,img,text")->select();
		if (!empty($result)) {
			$res['code'] = 1;
			$res['msg'] = 'success';
			$res['success'] = true; 
			$res['result'] = $result;
		}else{
			$res['code'] = 0;
			$res['msg'] = '访问出错';
		}
		$a = 'aaa';
		return json($res);
	}
}